<template>
  <div class="login-info">
    <el-row>
      <el-col :md="8">
        <h3 style="text-align: center;">用户权限管理系统</h3>
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="账号：" prop="username">
            <el-input v-model="ruleForm.username" autocomplete="off" clearable></el-input>
          </el-form-item>
          <el-form-item label="密码：" prop="password">
            <el-input
              type="password"
              v-model="ruleForm.password"
              autocomplete="off"
              show-password
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>
 
<script>
import { Message } from "element-ui";
export default {
  name: "Login",
  data() {
    var validateAccount = (rule, value, callback) => {
      if (value === "") {
        return callback(new Error("账号不能为空"));
      } else {
        callback();
      }
    };
    var validatePassword = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        username: "",
        password: ""
      },
      rules: {
        username: [
          {
            validator: validateAccount,
            trigger: "blur"
          }
        ],
        password: [
          {
            validator: validatePassword,
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      const param = new URLSearchParams();
      param.append("username", this.ruleForm.username);
      param.append("password", this.ruleForm.password);
      var that = this;
      that
        .$http({
          method: "post",
          // url: "http://localhost:8081/login",
          url:"/login",
          data: param
        })
        .then(res => {
          if (res.data == 1) {
            // console.log("-------" + this.ruleForm.username);
            localStorage.setItem("Name", this.ruleForm.username);
            this.$router.push({ path: "/home" });
          } else {
            Message({ message: "用户名或密码错误", type: "error" });
            this.ruleForm.username = "";
            this.ruleForm.password = "";
          }
        });
    }
  }
};
</script>
 
<style scoped>
.login-info >>> .el-col {
  background-color: #e5e8ec;
  padding: 2% 5% 0% 2%;
  width: 400px;
  position: relative;
  left: 50%;
  /* top: 50%; */
  transform: translate(-50%, 50%);
}
/* .login-info >>> .el-form-item{
  width:63%;
} */
</style>